<?php
/** 
 * $Source: /cvsroot/pn-commerce/pn-commerce/pntemplates/plugins/function.pncItemsPager.php,v $
 * $Author: landseer $
 * $Revision: 1.5 $
 * $Date: 2004/09/04 15:03:46 $
 *
 * Type: Function
 *
 * creates a complete html code for selecting the right "page to display if not all items should be shown
 * @param: total - total number of items
 * @param: perpage - items per page
 * @param: startnum - (optional) starting number for this page
 * @param: thispage - (optional) num of actual page
 * @param: maxlinks - (optional) num of pages to show, default is 10 pages
 * @return string 
 */
function smarty_function_pncItemsPager($params, &$smarty)
{
    extract($params);
    unset($params);

    // Quick check to ensure that we have work to do
    if ($total < $perpage)
    {
        return;
    }
    if (empty($startnum))
    {
        $startnum = 1;
    }

    if (empty($perpage))
    {
        $perpage = 10;
    }

    if (empty($maxlinks))
    {
        $maxlinks = 10;
    }
    
    $pager = "";
    // number of pages
    if( $total > $perpage )
    {
        $pages = ceil($total / $perpage);
        // we are on page $thispage
        $thispage = floor( $startnum / $perpage ) + 1;

        $left = "";
        $right = "";
        if($pages > $maxlinks)
        {
            $metapages = ceil( $pages / $maxlinks);
            $metapage = 1;
            $temp = $thispage;
            while($temp>$maxlinks)
            {
                $temp = $temp - $maxlinks;
                $metapage++;
            }
            $startpage = (($metapage-1) * $maxlinks) + 1;
            $endpage   = (($startpage+$maxlinks)>$pages) ? $pages : $startpage + $maxlinks -1;
            // left boundary
            if( $metapage <> 1 )
            {
                $left  = "<a href=\"".pnModURL( 'pncommerce', 'user', 'categoryview', array( 'page' => 1 ) )."\"><<</a> ";
                if($metapage <> 2)
                {
                    $left .= "<a href=\"".pnModURL( 'pncommerce', 'user', 'categoryview', array( 'page' => $thispage - $maxlinks ) )."\"><</a> ";
                }
            } 
            // right boundary
            if( $metapage <> $metapages )
            {
                if($metapage <> $metapages - 1)
                {
                    $right  = "<a href=\"".pnModURL( 'pncommerce', 'user', 'categoryview', array( 'page' => $thispage + $maxlinks ) )."\">></a> ";
                }
                $right .= "<a href=\"".pnModURL( 'pncommerce', 'user', 'categoryview', array( 'page' => $pages ) )."\">>></a> ";
            }
        }
        else
        {
            // there are max maxlinks pages to show
            $startpage = 1;
            $endpage = $pages;
            // << < previous pages 
            $left = "* ";
            $right = "*";
        }

        // pagenumbers
        for($i=$startpage;$i<=$endpage;$i++)
        {
            if( $thispage == $i )
            {
                $pager .= "<strong>$i</strong> ";
            }
            else
            {
                $pager .= "<a href=\"".pnModURL( 'pncommerce', 'user', 'categoryview', array( 'page' => $i ) )."\">$i</a> ";
            }
        }
        $pager = $left.$pager.$right;
    }
    return $pager;
}
?>
